1 00:00:00,700 --> 00:00:01,390 Okay. 2 00:00:01,390 --> 00:00:07,450 In this lecture, we're going to be filling out the play sounds handler along with our lightning module 3 00:00:07,450 --> 00:00:08,890 script on the client. 4 00:00:08,920 --> 00:00:14,320 Now, the purpose of this module script is to allow the server to tell specific players or all the players 5 00:00:14,320 --> 00:00:16,150 to play a sound on their end. 6 00:00:16,180 --> 00:00:20,680 It gives the server more fine tuned control over the sounds that a player needs to play on their end, 7 00:00:20,680 --> 00:00:25,060 and we're going to be using the same audio script in the main story part of our game, and the one we're 8 00:00:25,060 --> 00:00:28,570 going to script here is going to serve as the foundation for that other script. 9 00:00:28,570 --> 00:00:30,550 So let's go ahead and get started in here. 10 00:00:30,550 --> 00:00:34,090 All we're going to need is a reference to replicated storage. 11 00:00:37,390 --> 00:00:40,540 And then we're going to need a reference to the sound service. 12 00:00:44,120 --> 00:00:47,690 And then I'm going to create the table that's going to represent this module script. 13 00:00:47,690 --> 00:00:49,190 So we'll just call it sounds. 14 00:00:49,190 --> 00:00:52,850 And then we can return sounds at the end of our module script here. 15 00:00:53,500 --> 00:00:56,320 Now our module script is going to do two things. 16 00:00:56,320 --> 00:01:01,510 It's going to listen to an event within replicated storage when the server tells us to play a sound. 17 00:01:01,510 --> 00:01:07,000 And it's also going to have a while loop running here that randomly generates a lightning sounds just 18 00:01:07,000 --> 00:01:08,950 to add more ambience into our game. 19 00:01:08,980 --> 00:01:14,110 Now, in order to be able to reference this lightning module script here, we're going to have to get 20 00:01:14,110 --> 00:01:18,640 this folder or this client folder that actually houses all of the module scripts. 21 00:01:18,640 --> 00:01:24,040 So since our play sounds handler is going to be inside of our handlers folder, we need to get the parent 22 00:01:24,040 --> 00:01:29,890 of this script and then get the parent of this folder to be able to access all of the other module scripts 23 00:01:29,890 --> 00:01:30,940 that are in our game. 24 00:01:30,940 --> 00:01:35,350 So we can create a variable, I'm going to call it main script, and it's going to be equal to this 25 00:01:35,350 --> 00:01:38,950 script parent and get the parent of that parent. 26 00:01:38,950 --> 00:01:42,430 So that way we're referring to this client folder here. 27 00:01:43,050 --> 00:01:47,100 And then we can go ahead and require our lightning module script. 28 00:01:47,100 --> 00:01:54,900 So I can just call this lightning and require from our main script, we'll get the modules folder and 29 00:01:54,900 --> 00:01:56,970 get our lightning module script. 30 00:01:56,970 --> 00:02:00,420 Now currently it's empty but we're going to be filling this out in a moment. 31 00:02:00,420 --> 00:02:06,540 The next thing we need to make a reference to is the play sound event that is inside of replicated storage. 32 00:02:06,540 --> 00:02:14,070 So inside of the events folder and inside of remotes, there is a remote event called Play Sound. 33 00:02:14,070 --> 00:02:19,950 And then lastly inside of the sound service I have a section of all of the different thunder sounds 34 00:02:19,950 --> 00:02:20,610 that we can play. 35 00:02:20,610 --> 00:02:24,720 So there's three different ones, and we're going to randomly pick one of these lightning sounds and 36 00:02:24,720 --> 00:02:25,230 play it. 37 00:02:25,230 --> 00:02:28,380 So I'll create a variable called lightning sounds. 38 00:02:29,130 --> 00:02:35,190 And inside of sound service in the ambience folder get weather and then thunder. 39 00:02:35,370 --> 00:02:39,060 And then we can just get all of those sounds just like this. 40 00:02:39,180 --> 00:02:42,810 And then lastly I'm going to create a new random data type. 41 00:02:43,400 --> 00:02:49,490 Okay, so inside of this service or our handler, we need to have a function to initialize it. 42 00:02:49,490 --> 00:02:50,690 So init. 43 00:02:50,690 --> 00:02:55,190 And then we're going to have a function to start this module. 44 00:02:55,740 --> 00:02:59,880 Inside of this initialize section, all we're going to do is we're going to listen to that play sound 45 00:02:59,880 --> 00:03:00,360 event. 46 00:03:00,360 --> 00:03:03,300 So on client event we're going to connect a function to it. 47 00:03:03,930 --> 00:03:08,760 And what we should get past from the server is a sound instance it wants us to put. 48 00:03:09,320 --> 00:03:14,450 And then we can just go ahead and play that sound just in case the sound doesn't exist. 49 00:03:14,450 --> 00:03:16,730 We can just do, if not sound. 50 00:03:17,540 --> 00:03:21,380 Then we'll just return out of this function. 51 00:03:22,090 --> 00:03:26,470 And then inside of our start function, what we're going to do here is we're going to have a while loop 52 00:03:26,470 --> 00:03:31,960 that's going to use our RNG data type to randomly summon lightning. 53 00:03:32,440 --> 00:03:36,490 Now, because we're going to be using a while loop here, the main thread of execution is going to get 54 00:03:36,490 --> 00:03:37,510 stuck inside of it. 55 00:03:37,510 --> 00:03:41,110 So we need to actually spawn the while loop in another function. 56 00:03:41,110 --> 00:03:46,000 That way, you know, when we're setting up the client, we don't get stuck in this start function here. 57 00:03:46,000 --> 00:03:52,150 So what I could do is I could use task and I could use task dot defer because I don't need this, uh, 58 00:03:52,150 --> 00:03:54,130 to run immediately. 59 00:03:55,310 --> 00:03:59,870 And what we'll do in here is we'll have a while loop that runs forever. 60 00:04:00,140 --> 00:04:03,710 And what we could do is we could use our orange data type. 61 00:04:03,710 --> 00:04:06,650 So if RNG we could do one through 25. 62 00:04:06,650 --> 00:04:11,750 If it's equal to one, then we can go ahead and summon some lightning. 63 00:04:11,750 --> 00:04:15,080 And that's what we'll use our lightning module script for. 64 00:04:15,080 --> 00:04:17,240 So we'll have a function in there to summon lightning. 65 00:04:17,240 --> 00:04:19,160 So let's actually go ahead and fill that out. 66 00:04:19,670 --> 00:04:24,470 So inside of our lightning module script, the only service we're going to need is a reference to the 67 00:04:24,470 --> 00:04:25,760 lightning service. 68 00:04:25,760 --> 00:04:28,520 So game getservice lightning. 69 00:04:29,230 --> 00:04:34,600 And then we'll just call this module script lightning, and we'll make sure to return it at the end 70 00:04:34,600 --> 00:04:35,230 here. 71 00:04:35,860 --> 00:04:39,670 And the only public function we're going to have in here is to summon lightning. 72 00:04:39,670 --> 00:04:42,790 So we could call this function Summon Lightning. 73 00:04:44,340 --> 00:04:47,280 And we can do whatever inside of here. 74 00:04:47,310 --> 00:04:50,400 Now I'm going to create a dedicated function as well for this. 75 00:04:50,400 --> 00:04:52,770 Again I'm going to call it Summon Lightning. 76 00:04:52,890 --> 00:04:59,910 And what we're going to do in here is we're going to use, uh, the random data type again, and set 77 00:04:59,910 --> 00:05:04,500 the brightness in the workspace to random different intervals of numbers. 78 00:05:04,500 --> 00:05:07,770 That way the outside looks like there was a flash of light. 79 00:05:08,130 --> 00:05:12,030 So we need to keep track of the original brightness inside of the lighting service. 80 00:05:12,030 --> 00:05:14,250 So we'll have a variable to do that. 81 00:05:14,460 --> 00:05:18,570 So inside of the lightning service we'll get the original brightness that is in our game. 82 00:05:18,570 --> 00:05:22,440 And then I'm also going to create a variable for a new random data type. 83 00:05:22,650 --> 00:05:27,960 And then inside of here what I'm going to do is I'm going to generate a new random number using RNG. 84 00:05:28,140 --> 00:05:29,460 Next number. 85 00:05:31,920 --> 00:05:38,400 And we can get a random number between 0 and 12. 86 00:05:38,700 --> 00:05:42,960 And this is going to be the brightness we're going to set inside of our game. 87 00:05:42,960 --> 00:05:45,870 So we'll up the brightness anywhere between 8 and 12. 88 00:05:45,870 --> 00:05:51,090 So what we'll do is we'll do lighting service dot brightness is equal to this new random number. 89 00:05:51,090 --> 00:05:54,870 Or actually we can add this to the brightness. 90 00:05:54,870 --> 00:05:59,010 So this new random number we're going to add to the current brightness in our game. 91 00:05:59,220 --> 00:06:04,710 So since the brightness is currently zero what it'll do is it'll pump up the brightness to like eight. 92 00:06:04,710 --> 00:06:08,310 So it looks very bright out like a flash of light just happened. 93 00:06:08,430 --> 00:06:13,380 And then like a little bit of time later we'll drop it down to like three and then go back up to make 94 00:06:13,380 --> 00:06:16,440 it look like it's flickering outside, like there was a lightning strike. 95 00:06:16,440 --> 00:06:24,810 So we can wait a random amount of time, like anywhere between, uh, we could do next number and do 96 00:06:24,810 --> 00:06:30,240 a very short interval of time, like 0.08 seconds or up to 0.12 seconds. 97 00:06:30,480 --> 00:06:33,750 And then we're going to set the brightness in the workspace again. 98 00:06:34,370 --> 00:06:38,570 So instead of adding the random number again, what we could do is we could subtract it by this random 99 00:06:38,570 --> 00:06:39,470 number again. 100 00:06:39,470 --> 00:06:44,450 But I don't want the workspace to go back to its original brightness so we can reduce this number by 101 00:06:44,450 --> 00:06:45,740 something like four. 102 00:06:46,310 --> 00:06:50,450 So first the workspace gets bright and then it gets a bit dimmer. 103 00:06:50,450 --> 00:06:53,810 And then we'll have it get brighter again and then more dimmer. 104 00:06:53,810 --> 00:06:55,610 And then set it back to the original brightness. 105 00:06:55,610 --> 00:07:00,470 So again we can wait for a random amount of time RNG next number. 106 00:07:00,770 --> 00:07:05,960 This time we could probably again wait for 0.08 to 0.12. 107 00:07:06,410 --> 00:07:13,010 And this time what we could do is we could set the brightness in the workspace to something like the 108 00:07:13,010 --> 00:07:18,410 random number, and we can add a number like six, I don't know, just throwing out random numbers. 109 00:07:20,320 --> 00:07:20,860 RNG. 110 00:07:21,190 --> 00:07:22,900 Next number we're going to wait again. 111 00:07:22,900 --> 00:07:29,200 This time we can wait a little bit longer, maybe anywhere from 0.08 seconds to 0.15. 112 00:07:29,710 --> 00:07:33,460 Again, we're going to decrease the brightness in the workspace. 113 00:07:33,460 --> 00:07:37,180 So we could subtract this by like a number like eight. 114 00:07:38,020 --> 00:07:38,590 That way. 115 00:07:38,590 --> 00:07:42,280 At least it's larger than what was added to the brightness. 116 00:07:42,280 --> 00:07:44,290 So we're still dimming the brightness down. 117 00:07:44,290 --> 00:07:47,440 And then again, we're going to wait a random amount of time. 118 00:07:47,650 --> 00:07:52,630 And then finally we can set the lighting service dot brightness back to the original brightness. 119 00:07:52,630 --> 00:08:00,430 So this is just kind of imitating the lightning or the light in our game that would imitate lightning. 120 00:08:00,430 --> 00:08:03,910 And then we can literally just call this function summoned lightning. 121 00:08:04,240 --> 00:08:04,600 All right. 122 00:08:04,600 --> 00:08:06,580 So back to our play sounds handler. 123 00:08:06,580 --> 00:08:12,970 What we can do is we can now call this summon lightning function to flicker the lighting in the workspace. 124 00:08:12,970 --> 00:08:17,170 And then what we're going to do is we're going to actually wait a random amount of time. 125 00:08:17,170 --> 00:08:22,510 So we can use RNG next number and wait anywhere from like 0.1 seconds all the way up to something like 126 00:08:22,510 --> 00:08:23,860 1.5 seconds. 127 00:08:23,860 --> 00:08:30,760 And this is to imitate how long it takes for sound to reach the player after we see the flash of light, 128 00:08:30,760 --> 00:08:34,060 because they're not going to hear the sound instantly if the lightning is super far away. 129 00:08:34,060 --> 00:08:35,080 So we're just going to use this. 130 00:08:35,110 --> 00:08:41,980 Wait here to imitate the sound of, uh, having to travel and then reach the eardrum of our player. 131 00:08:42,560 --> 00:08:48,050 So once we wait that, then we can access our lightning sounds and pick a random one out of there so 132 00:08:48,050 --> 00:08:55,430 we can use RNG next integer and pass one to the max number of lightning sounds that we have, which 133 00:08:55,430 --> 00:08:56,210 would be three. 134 00:08:56,210 --> 00:08:59,240 And we're just going to play a random one of those sounds. 135 00:08:59,240 --> 00:09:04,520 And then after that, we're going to wait for one second in this loop because we don't want to have 136 00:09:04,520 --> 00:09:06,530 a while loop that's running at the speed of light. 137 00:09:06,530 --> 00:09:07,160 Right. 138 00:09:07,160 --> 00:09:12,170 Because if we didn't put this wait statement here, then this if statement is basically useless because 139 00:09:12,170 --> 00:09:17,540 this while loop will be running so fast, that will be eventually guaranteed to hit one. 140 00:09:17,540 --> 00:09:19,790 So we'll put this cooldown in here. 141 00:09:19,790 --> 00:09:25,280 That way lightning isn't playing all of the time, and that's basically all we need to do for our play 142 00:09:25,280 --> 00:09:27,590 sounds handler and our lightning module script. 143 00:09:27,590 --> 00:09:32,570 So if we go and test out our game, actually let me reset the brightness in the workspace back to zero. 144 00:09:32,690 --> 00:09:37,340 If we test this out, then we should get some random lightning playing in our game eventually. 145 00:09:39,840 --> 00:09:43,140 So as you can see, the outside flickered and then we get our lightning. 146 00:09:45,950 --> 00:09:48,530 Click it again outside and got our lightning. 147 00:09:49,130 --> 00:09:51,110 All right so that's all for this lecture. 148 00:09:51,110 --> 00:09:53,150 We're getting pretty close to wrapping up our lobby. 149 00:09:53,150 --> 00:09:56,120 But the bigger project for our game is still ahead of us. 150 00:09:56,120 --> 00:09:58,580 Anyways I'll see you in the next lecture.